package com.yaoran.userauth.controller;

import com.yaoran.userauth.entity.User;
import com.yaoran.userauth.service.UserService;
import com.yaoran.userauth.utils.PageUtils;
import com.yaoran.userauth.utils.Query;
import com.yaoran.userauth.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    private  final static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public R userAdd(@RequestBody User user){
        userService.addUser(user,null);
        return R.ok();
    }

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public R<PageUtils<User>> listUser(@RequestBody Map<String,String> params){
        Map<String,Object> query = new HashMap<>();
        query.put("page",params.get("page"));
        query.put("limit",params.get("limit"));
        String keyWord = params.get("keyWord");
        PageUtils<User> pageUtils = userService.listUser(new Query(query),keyWord);
        return R.ok(pageUtils);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    public R updateUser(@RequestBody User user){
        userService.updateUser(user);
        return R.ok();
    }

    @ApiOperation("删除")
    @PostMapping("/del")
    public R deleteUser(@RequestBody Map<String,Long> params){
        userService.deleteUser(params.get("id"));
        return R.ok();
    }

    @ApiOperation("批量删除")
    @PostMapping("/dels")
    public R deleteManyUser(@RequestBody Map<String,Long[]> params){
        userService.deleteManyUser(params.get("ids"));
        return R.ok();
    }

    @ApiOperation("用户详情")
    @GetMapping("/detail")
    public R<User> getUser(@RequestParam Long id){
        User user = userService.getUser(id);
        return R.ok(user);
    }
}
